﻿'Description:槽液基础数据类
'Copyright (c) : 通力凯顿（北京）系统集成有限公司
'Writer:Zhangrj
'create Date:2022-11-21
'Rewriter:
'Rewrite Date:
Imports BusinessRule.DTO
Imports External.APICalling
Imports DatabaseAccess.DBUtility
Imports System.Text

Public Class uLMSBathResMonitorBusiness
    ''' <summary>
    ''' 获取槽液化验设备检测数据
    ''' </summary>
    ''' <param name="para"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetBathResMonitorListByPage(ByVal para As Dictionary(Of String, String)) As uMESProcessResult
        Dim strSql = <string>
                         SELECT brm.*,r.resourcename,DECODE(brm.isenable,'1','是','0','否','') isenablename,
                         (CASE WHEN brm.expiredate>=SYSDATE THEN '有效' ELSE '无效' end) status,
                         brm.checkcycle || '日' checkcyclename 
                         FROM bathResMonitorInfo brm 
                         LEFT JOIN resourcedef r ON r.resourceid = brm.resourceid
                         WHERE brm.isuse = 0 AND r.factoryid = '{0}'
         </string>.Value
        strSql = String.Format(strSql, para("FactoryID"))
        If para.ContainsKey("Resource") Then
            If Not String.IsNullOrWhiteSpace(para("Resource")) Then
                strSql += String.Format(" AND (r.resourcename LIKE '{0}%' OR r.description LIKE '{0}%')", para("Resource"))
            End If
        End If

        If para.ContainsKey("WorkCenterCode") Then
            If Not String.IsNullOrWhiteSpace(para("WorkCenterCode")) Then
                strSql += String.Format(" and brm.resworkcentercode like '{0}%' ", para("WorkCenterCode"))
            End If
        End If

        If para.ContainsKey("Status") Then
            If Not String.IsNullOrWhiteSpace(para("IsEnable")) Then
                If para("Status") = "1" Then '有效日期
                    strSql += " AND brm.expiredate >= SYSDATE "
                Else
                    strSql += " AND brm.expiredate < SYSDATE "
                End If
            End If
        End If

        If para.ContainsKey("IsEnable") Then
            If Not String.IsNullOrWhiteSpace(para("IsEnable")) Then
                strSql += String.Format(" and brm.isenable = '{0}' ", para("IsEnable"))
            End If
        End If

        strSql += " ORDER BY brm.resourceid,brm.resworkcentercode"

        Dim paraPage As New uMESPagingDataDTO
        paraPage.CurrentPageIndex = Convert.ToInt32(para("PageNumber").ToString)
        paraPage.PageSize = Convert.ToInt32(para("PageSize").ToString)
        paraPage.strSQL = strSql.ToString
        Return DatabaseHelper.RunProcedureForPage(paraPage)
    End Function

    ''' <summary>
    ''' 获取槽液化验设备检测送检详细数据列表
    ''' </summary>
    ''' <param name="para"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function GetBathResMonitorCheckListByPage(ByVal para As Dictionary(Of String, String)) As uMESProcessResult
        Dim strSql = <string>
                         SELECT brm.resworkcentercode,brm.ressourcedesc,brm.expiredate,ceil(brm.expiredate - SYSDATE) AS expiretip,brm.bathresmonitorinfoid,
                         brc.bathresmonitorcheckno,brc.sendcheckdate,brc.status,DECODE(brc.status,'0','送检中','5','检验完成','') statusName,
                         brc.createempname,brc.createdate,brc.lastchangeempname,brc.lastchangedate,brc.checkdocpath,'' checkdocFileName,brc.bathresmonitorcheckid
                         FROM bathResMonitorInfo brm
                         LEFT JOIN bathresmonitorcheck brc ON brc.bathresmonitorinfoid = brm.bathresmonitorinfoid AND brc.isuse = 0
                         LEFT JOIN resourcedef r ON r.resourceid = brm.resourceid
                         WHERE brm.isuse = 0 AND r.factoryid = '{0}' AND brm.isenable = 1
         </string>.Value
        strSql = String.Format(strSql, para("FactoryID"))
        If para.ContainsKey("Resource") Then
            If Not String.IsNullOrWhiteSpace(para("Resource")) Then
                strSql += String.Format(" and (r.resourcename LIKE '%{0}%' OR r.description LIKE '%{0}%')", para("Resource"))
            End If
        End If

        If para.ContainsKey("WorkCenterCode") Then
            If Not String.IsNullOrWhiteSpace(para("WorkCenterCode")) Then
                strSql += String.Format(" and brm.resworkcentercode like '%{0}%' ", para("WorkCenterCode"))
            End If
        End If

        If para.ContainsKey("Status") Then
            If Not String.IsNullOrWhiteSpace(para("Status")) Then
                strSql += String.Format(" and brc.status ='{0}' ", para("Status"))
            End If
        End If

        If para.ContainsKey("SendCheckDate") Then
            If Not String.IsNullOrWhiteSpace(para("SendCheckDate")) Then
                strSql += String.Format(" AND to_char(brc.sendcheckdate,'YYYY-MM-DD') = to_char(to_date('{0}','YYYY-MM-DD'),'YYYY-MM-DD')", para("SendCheckDate"))
            End If
        End If

        If para.ContainsKey("ChangeDate") Then
            If Not String.IsNullOrWhiteSpace(para("ChangeDate")) Then
                strSql += String.Format(" AND to_char(brc.lastchangedate,'YYYY-MM-DD') = to_char(to_date('{0}','YYYY-MM-DD'),'YYYY-MM-DD')", para("ChangeDate"))
            End If
        End If

        strSql += " ORDER BY brm.resourceid,brm.resworkcentercode,brc.createdate DESC"

        If para.ContainsKey("OverdueStatus") Then
            If Not String.IsNullOrWhiteSpace(para("OverdueStatus")) Then
                If para("OverdueStatus") = "0" Then '超时
                    strSql = "SELECT * FROM (" + strSql + ")t where t.expiretip <= 0"
                ElseIf para("OverdueStatus") = "1" Then '1日到期
                    strSql = "SELECT * FROM (" + strSql + ")t where t.expiretip > 0 AND t.expiretip <= 1"
                ElseIf para("OverdueStatus") = "3" Then '3日到期
                    strSql = "SELECT * FROM (" + strSql + ")t where t.expiretip > 1 AND t.expiretip <= 3"
                End If
            End If
        End If

        Dim paraPage As New uMESPagingDataDTO
        paraPage.CurrentPageIndex = Convert.ToInt32(para("PageNumber").ToString)
        paraPage.PageSize = Convert.ToInt32(para("PageSize").ToString)
        paraPage.strSQL = strSql.ToString
        Return DatabaseHelper.RunProcedureForPage(paraPage)
    End Function

    ''' <summary>
    ''' 验证设备是否存在有效的槽液化验
    ''' </summary>
    ''' <param name="resourceID"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function IsVaildResMonitorCheck(ByVal resourceID As String) As Boolean
        Dim strSql = String.Format("SELECT b.expiredate FROM bathResMonitorInfo b WHERE b.resourceid = '{0}' and b.isenable = 1 and b.isuse = 0", resourceID)
        Dim dt As DataTable = DatabaseHelper.Query_ReturnDatTable(strSql)
        '如果该设备没有槽液化验数据，则有效
        If dt.Rows.Count = 0 Then
            Return True
        End If
        Dim expiredate As Date = Date.Parse(dt.Rows(0)("expiredate").ToString())
        '如果到期日期大于当前日期，则有效
        If expiredate > Date.Now Then
            Return True
        Else
            Return False
        End If
    End Function
End Class
